package com.qf.mybatisplus.utils;


import com.alibaba.fastjson.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;

//import net.sf.json.JSONObject;

/**
 * @author code
 */
public class JsonUtil {
    private final static Logger logger = LoggerFactory.getLogger(JsonUtil.class);
    /**
     * 此方法用于更换jsonObject的名字
     * @author 陈焯
     * @date 2019-06-21
     */
    public static com.alibaba.fastjson.JSONObject changeJsonObj(com.alibaba.fastjson.JSONObject jsonObj, Map<String, String> keyMap) {
        com.alibaba.fastjson.JSONObject resJson = new com.alibaba.fastjson.JSONObject();
        Set<String> keySet = jsonObj.keySet();
        for (String key : keySet) {
            String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
            try {
                com.alibaba.fastjson.JSONObject jsonobj1 = jsonObj.getJSONObject(key);
                resJson.put(resKey, changeJsonObj(jsonobj1, keyMap));
            } catch (Exception e) {
                try {
                    JSONArray jsonArr = jsonObj.getJSONArray(key);
                    resJson.put(resKey, changeJsonArr(jsonArr, keyMap));
                } catch (Exception x) {
                    resJson.put(resKey, jsonObj.get(key));
                }
            }
        }
        return resJson;
    }

    /**
     * 此方法用于更换jsonArray的名字
     * @author 陈焯
     * @date 2019-06-21
     */
    public static JSONArray changeJsonArr(JSONArray jsonArr, Map<String, String> keyMap) {
        JSONArray resJson = new JSONArray();
        for (int i = 0; i < jsonArr.size(); i++) {
            com.alibaba.fastjson.JSONObject jsonObj = jsonArr.getJSONObject(i);
            resJson.add(changeJsonObj(jsonObj, keyMap));
        }
        return resJson;
    }






}
